#include "ToolBar.h"
#include <QMouseEvent>
#include "MyButton.h"

ToolBar::ToolBar(QWidget* parent)
{
	this->setParent(parent);
	this->m_ui.setupUi(this);
	this->move(0, 0);
	this->resize(parent->size());
	this->m_closeWindowButton = new MyButton(QString("close"), 22, 22, this->m_ui.closeWidget);
	this->connect(this->m_closeWindowButton, &QPushButton::clicked, this, &ToolBar::clickCloseButton);
	// this->connect(this->m_ui.closeButton, &QPushButton::clicked, this, &ToolBar::clickCloseButton);
}

void ToolBar::mouseMoveEvent(QMouseEvent* ev)
{
	if (this->m_point != nullptr) {
		emit this->moveMainWindow(ev->globalPos() - *this->m_point);
	}
	// QWidget::mouseMoveEvent(ev);
}

void ToolBar::mousePressEvent(QMouseEvent* ev)
{
	if (ev->button() == Qt::LeftButton) {
		this->clearPoint();
		this->m_point = new QPoint(ev->pos());
	}
	// QWidget::mousePressEvent(ev);
}

void ToolBar::mouseReleaseEvent(QMouseEvent* ev)
{
	if (ev->button() == Qt::LeftButton) {
		// emit this->moveMainWindow(ev->globalPos() - *this->m_point);
		this->clearPoint();
	}
	// QWidget::mouseReleaseEvent(ev);
}

void ToolBar::clickCloseButton() {
	emit this->closeMainWindow();
}

void ToolBar::clearPoint()
{
	delete this->m_point;
	this->m_point = nullptr;
}

ToolBar::~ToolBar()
{
	delete this->m_closeWindowButton;
	this->clearPoint();
}
